//
//  PaoMaDengView.m
//  跑马灯
//
//  Created by huahua on 16/12/28.
//  Copyright © 2016年 os1. All rights reserved.
//

#import "PaoMaDengView.h"

@interface PaoMaDengView ()
{
    CGFloat labelH;
}
@property(nonatomic,strong) NSTimer* timer;// 定义定时器
@property(nonatomic,strong) UIView *viewAnima; //装滚动视图的容器
@property(nonatomic,weak) UILabel *customLab;  //显示文字的label

@end

@implementation PaoMaDengView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIView *viewAnima = [[UIView alloc] initWithFrame:frame];
        viewAnima.backgroundColor = [UIColor blackColor];
        viewAnima.clipsToBounds = YES;
        
        [self addSubview:viewAnima];
        self.viewAnima = viewAnima;
        
        UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectZero];
        [customLab setTextColor:[UIColor whiteColor]];
        [customLab setText:@"请在输入框中输入文字！"];
        customLab.font = [UIFont boldSystemFontOfSize:20.0];
        
        if (self.frame.size.height <= 414) {
            
            labelH = self.frame.size.height;
        }else
        {
            labelH = self.frame.size.width;
        }
        //根据字体得到nsstring的尺寸
        CGSize size = [customLab.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:customLab.font,NSFontAttributeName, nil]];
        
        customLab.frame = CGRectMake(self.viewAnima.frame.size.width, (self.viewAnima.frame.size.height-labelH)*0.5, size.width+20, labelH);
       
        [self.viewAnima addSubview:customLab];
        self.customLab = customLab;
        
        // 启动NSTimer定时器来改变UIImageView的位置
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.08
                                                      target:self selector:@selector(changePos)
                                                    userInfo:nil repeats:YES];
        
    }
    return self;
}

- (void)changePos
{
    CGPoint curPos = self.customLab.center;
    CGFloat lableY = (self.viewAnima.frame.size.height-labelH)*0.5;
    // 当curPos的x坐标已经超过了屏幕的宽度
    if(curPos.x < -self.customLab.frame.size.width*0.5)
    {
        CGFloat jianJu = self.customLab.frame.size.width/2;
        // 控制蝴蝶再次从屏幕左侧开始移动
        self.customLab.center = CGPointMake(self.viewAnima.frame.size.width + jianJu, labelH*0.5+lableY);
    }
    else
    {
        // 通过修改iv的center属性来改变控件的位置
        [UIView animateWithDuration:0.35 animations:^{
            
            self.customLab.center = CGPointMake(curPos.x - 5, labelH*0.5+lableY);
        }];
    }
}

- (void)setTitle:(NSString *)title{
    
    _title = title;
    self.customLab.text = _title;
    if (self.frame.size.height <= 414) {
        
        labelH = self.frame.size.height;
    }else
    {
        labelH = self.frame.size.width;
    }
    
    //根据字体得到nsstring的尺寸
    NSInteger font;
    if (self.textFont == 0 || !self.textFont) {
        
        font = 20;
    }else{
        
        font = self.textFont;
    }
    
    CGSize size = [_title sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font],NSFontAttributeName, nil]];
    self.customLab.frame = CGRectMake(self.viewAnima.frame.size.width, (self.viewAnima.frame.size.height-labelH)*0.5, size.width+20, labelH);
    
}

- (void)setTextFont:(NSInteger)textFont{
    
    _textFont = textFont;
    self.customLab.font = [UIFont systemFontOfSize:_textFont];
}

- (void)setTextColor:(UIColor *)textColor{
    
    _textColor = textColor;
    self.customLab.textColor = _textColor;
}

- (void)setBgColor:(UIColor *)bgColor{
    
    _bgColor = bgColor;
    self.viewAnima.backgroundColor = _bgColor;
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    self.viewAnima.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    
    if (self.frame.size.height <= 414) {
        
        labelH = self.frame.size.height;
    }else
    {
        labelH = self.frame.size.width;
    }
    
    //根据字体得到nsstring的尺寸
    if (self.title) {
        
        CGSize size = [self.title sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:self.textFont],NSFontAttributeName, nil]];
        self.customLab.frame = CGRectMake(self.viewAnima.frame.size.width, (self.viewAnima.frame.size.height-labelH)*0.5, size.width+20, labelH);
    }
    
}

@end
